package com.aaa.controller;

import com.aaa.entity.Dept;
import com.aaa.entity.MyReturn;
import com.aaa.service.DeptService;
import com.aaa.util.MyTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @author ：admin
 * @date ：Created in 2022/6/27 20:44
 * @description：
 * @modified By：
 */
@Controller
@RequestMapping("dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    MyReturn myReturn = new MyReturn();


    /**
     *  跳转到显示所有用户的页面
     * @return  String
     */
    @RequestMapping("toShowDept.do")
    public String toShowUser(){
        return "showDept";
    }

    @RequestMapping("findAllDept.do")
    @ResponseBody
    public MyReturn findAllDept(){
        List<Dept> deptList = deptService.findAllDept();
        myReturn.setData(deptList);
        MyTools.getSuccessResult(myReturn);
        System.out.println("111111");
        return myReturn;
    }

    /**
     * 添加部门
     * @param dept  实体
     * @return  MyReturn
     */
    @RequestMapping("saveDept.do")
    @ResponseBody
    public MyReturn saveDept(Dept dept) {

        //得到创建时间
        Date date = new Date();

        //将生成创建时间重新存入user实体中
        dept.setCreateTime(date);

        //将实体存入数据库
        int saveNum = deptService.saveDept(dept);

        //将插入的结果返回到前端页面，json，<状态码，消息，数据>
        if (saveNum > 0) {
            MyTools.getSuccessResult(myReturn);
        } else {
            MyTools.getFailResult(myReturn);
        }
        return myReturn;
    }

    /**
     * 删除部门
     * @param   deptId 数组
     * @return  MyReturn
     */
    @RequestMapping("deleteDeptById.do")
    @ResponseBody
    public MyReturn deleteDeptById(@RequestParam Integer[] deptId) {
        int delNum = 0;
        for (Integer id : deptId) {
            deptService.deleteDeptById(id);
            delNum++;
        }
        if (delNum == deptId.length) {
            MyTools.getSuccessResult(myReturn);
        } else {
            MyTools.getFailResult(myReturn);
        }
        return myReturn;
    }

    /**
     * 删除部门
     * @param   dept 实体
     * @return  MyReturn
     */
    @RequestMapping("updateDept.do")
    @ResponseBody
    public MyReturn updateDept(Dept dept) {
        //得到修改时间
        Date date = new Date();
        dept.setUpdateTime(date);
        int updateNum = deptService.updateDept(dept);
        if (updateNum > 0){
            MyTools.getSuccessResult(myReturn);
        }else {
            MyTools.getFailResult(myReturn);
        }
        return myReturn;
    }




}
